import { useState, useEffect } from 'react';
import { history } from 'umi'
import { fetchVersionList } from '../Version/service';

// 产品版本列表
const useVersionList = query => {
    const [resData, setResData] = useState([]);
    const [total, setTotal] = useState(0);
    const [loading, setLoading] = useState(true);
    const [mounted, setMounted] = useState(false);

    const fetchData = async () => {
        setLoading(true);
        if (mounted) {
            history.push({ pathname: window.location.pathname, query });
        }
        const { data, code } = await fetchVersionList(query);
        if (code === 10000) {
            setResData(data.rows);
            setTotal(data.total);
            setMounted(true);
        }
        setLoading(false);
    };

    useEffect(() => {
        fetchData();
    }, [query]);

    return [loading, resData, total];
};

export default useVersionList;
